<?php


namespace App\Admin\Extension;


use App\Models\CouponCode;
use Dcat\Admin\Widgets\Metrics\Card;
use Illuminate\Http\Request;

class CouponCodeSmallCard extends Card
{

    protected $coupon_type_id = 0;

    // 保存自定义参数
    protected $data = [];

    // 构造方法参数必须设置默认值
    public function __construct(array $data = [])
    {
        $this->data = $data;

        parent::__construct();
    }

    /**
     * 初始化卡片内容
     *
     * @return void
     */
    protected function init()
    {

        parent::init();

        $this->title('使用记录');

    }

    // 传递自定义参数到 handle 方法
    public function parameters() : array
    {
        return $this->data;
    }


    public function addScript()
    {
        parent::addScript(); // TODO: Change the autogenerated stub
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {

        $this->coupon_type_id = $request->get('type_id');
    }

    public function renderContent()
    {
        parent::renderContent(); // TODO: Change the autogenerated stub

        $couponCodesInfo = $this->getCodes();

        $html = $this->getTableHeaderHtml();
        $html .= $couponCodesInfo['html'];
        $html .= $this->getTableHeaderBottomHtml();
        $html .= $this->getPaginateHtml($couponCodesInfo['couponCodesPaginateObj']);


        return $html;
    }


    private function getTableHeaderHtml()
    {
        return '<table class="table custom-data-table data-table" id="grid-table">
            <thead>
                        <tr>
                                    <th>ID</th>
                                    <th>卡密</th>
                                    <th>有效期</th>
                                    <th>核销时间</th>
                            </tr>
            </thead>

            
            <tbody>';
    }

    private function getCodes()
    {

        $couponCodesPaginateObj = CouponCode::where('coupon_type_id', $this->coupon_type_id)
            ->whereNotNull('use_time')->paginate(20);

        $html = '';
        foreach ($couponCodesPaginateObj as $couponCode) {
            $html .= "<tr><td>".$couponCode->id."</td>
                                            <td>".$couponCode->card_secret."</td>
                                            <td>30月</td>
                                            <td>".$couponCode->use_time."</td>
                                    </tr>";
        }

        return [
            'couponCodesPaginateObj'   =>  $couponCodesPaginateObj,
            'html'          =>  $html
        ];
    }

    private function getTableHeaderBottomHtml()
    {
        return '</tbody></table>';
    }

    private function getPaginateHtml($couponCodesPaginateObj)
    {

        $html = '<div class="box-footer d-block clearfix ">
            <span class="d-none d-sm-inline" style="line-height:33px;color:#7c858e">从 <b>1</b> 到 <b>'.
            $couponCodesPaginateObj->count().'</b> ，总共 <b>'.$couponCodesPaginateObj->lastPage().'</b> 条</span>
            <ul class="pagination pagination-sm no-margin pull-right shadow-100" style="border-radius: 1.5rem">
            ';
        if ($couponCodesPaginateObj->currentPage() == $couponCodesPaginateObj->perPage()) {
            $html .= '<li class="page-item previous disabled"><span class="page-link"></span></li>';
        } else {
            $html .= '<li class="page-item previous"><span class="page-link"></span></li>';
        }
        $html .= '<li class="page-item active"><span class="page-link">'.$couponCodesPaginateObj->currentPage().'</span></li>';
        if ($couponCodesPaginateObj->currentPage() == $couponCodesPaginateObj->lastPage()) {
            $html .= '<li class="page-item next disabled"><span class="page-link"></span></li>';
        } else {
            $html .= '<li class="page-item next"><span class="page-link"></span></li>';
        }
        $html .= '</ul></div>';

        return $html;
    }

}